<?php
$isReloadable = true;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../../_inc/funct.tabDati.php');

if (reqVar('azione') == 'elimina') {
	$msgPag .= confElim();
} 
$sqlSel = "SELECT * FROM rgl_azienda " .
		" WHERE id_azienda = '".reqVarAS('id')."'";
$objDB->query($sqlSel,'selRec');
if (!$acRec = $objDB->fetch('selRec')) {
	backOnErr("var. id '".reqVar('id')."' mancante o errato");
}
$_REQUEST = array_merge($acRec,$_REQUEST);

$sCmd1L = "";
if (true) {
	$sCmd1L .= "<a class='cmd1' href='xbrl01BilIns.php?id_azienda=".reqVar('id')."&$sXSNQS' >carica bilanci file XBRL</a>";
	$sCmd1L .= "&nbsp;&nbsp;&nbsp;";
}
$sCmd2L = "";
if (true) {
	$sCmd2L .= "<a class='cmd1' href='../crat/rglobIns.php?id=".reqVar('id')."&$sXSNQS' >nuovo rating</a>";
	$sCmd2L .= "&nbsp;&nbsp;&nbsp;";
}
$sCmd1R = '';
if (true) {
	$sCmd1R .= "&nbsp;&nbsp;&nbsp;";
	$sCmd1R .= "<a class='cmd1' href='anagMod.php?id=".reqVar('id')."&$sXSNQS' >modifica</a>";
	if (true) {
		$sCmd1R .= "&nbsp;&nbsp;&nbsp;";
		$sCmd1R .= "<a class='cmd1' href='#' onclick='confElim();' >elimina</a>";
	}
}
$sCmd1R .= cmdTorna(); 
$sCmd2R = cmdTorna(); 

testaHtml('', $msgPag, 'ratg');
?>
<script language="JavaScript" type="text/javascript">
  	function confElim() {
  		if (confirm("Confermi l'eliminazione dell'anagrafica ?")) {
  			document.location.href = "<?=$_SERVER['PHP_SELF']."?id=".reqVar('id')."&azione=elimina&".$sXSNQS?>";
  		}
  	}
</script>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="cmd1" align="right" colspan='20'><?=$sCmd1R?></td>
   </tr>
   <tr>
      <td class="tit1" colspan='20' align='center'>Azienda</td>
   </tr>
<?
$aCamForm = array();
require("_inc/const.campiAnag.php");
echo getHtmlCampiForm($aCamForm,'vis');
?>
   <tr>
      <td class="cmd1" align="left" colspan='10'><?=$sCmd1L?></td>
      <td class="cmd1" align="right" colspan='10'><?=$sCmd1R?></td>
   </tr>
   <tr>
      <td class="tit1" align="center" colspan='20'>Bilanci</td>
   </tr>
	<tr>
		<td class='lab1' colspan='3' align='center'>Data rif.</td>
		<td class='lab1' colspan='3' align='center'>Fonte</td>
		<td class='lab1' colspan='11' align='center'>Descrizione</td>
		<td class='lab1' colspan='3' align='center'>Stato</td>
	</tr>
<?
$sSql = "SELECT * FROM rgl_bilancio "
	." WHERE id_azienda = ".reqVar('id')." ORDER BY data_rif DESC,id_bil DESC ";
$objDB->query($sSql,'listBR');
while($aB=$objDB->fetch('listBR')) {
?>
	<tr>
		<td class='txt1' colspan='3' align='center'>
			<a href='bilCeeVis.php?id=<?=$aB['id_bil']?>&xsn=<?=XSN?>' class='txt1'>
			<?=YMD2DMY($aB['data_rif'])?>
			</a>
		</td>
		<td class='txt1' colspan='3'><?=$aB['fonte_bil']?></td>
		<td class='txt1' colspan='11'><?=$aB['descr']?></td>
		<td class='txt1' colspan='3'><?=$aB['stato_lav']?></td>
	</tr>
<?
}
?>
   <tr>
      <td class="cmd1" align="left" colspan='10'><?=$sCmd2L?></td>
      <td class="cmd1" align="right" colspan='10'><?=$sCmd2R?></td>
   </tr>
   <tr>
      <td class="tit1" align="center" colspan='20'>Rating</td>
   </tr>
   <tr>
      <td class="lab1" align="center" colspan='1'>id.</td>
      <td class="lab1" align="center" colspan='5'>Descrizione</td>
      <td class="lab1" align="center" colspan='2'>Rating</td>
      <td class="lab1" align="center" colspan='2'>Data rif.</td>
      <td class="lab1" align="center" colspan='2'>Sett.</td>
      <td class="lab1" align="center" colspan='2'>R.Bil.</td>
      <td class="lab1" align="center" colspan='2'>R.Quest.</td>
      <td class="lab1" align="center" colspan='2'>Ce.Ri.</td> 
      <td class="lab1" align="center" colspan='2'>Stato</td>     
   </tr>
<?
$sSql = "SELECT r.*, mg.descr AS mg_descr, mg.id_mod_rque, mg.id_mod_rbil, rb.sigla AS rb_sigla, b.data_rif AS b_data_rif "
	.", rq.sigla AS rq_sigla "
	." FROM rgl_rating r"
	." INNER JOIN rgl_mod_rglob mg ON mg.id_mod_rglob = r.id_mod_rglob "
	." LEFT JOIN rgl_ratbil rb ON rb.id_rating = r.id_rating "
	." LEFT JOIN rgl_bilancio b ON b.id_bil = rb.id_bil "
	." LEFT JOIN rgl_ratque rq ON rq.id_rating = r.id_rating "
	." WHERE r.id_azienda = ".reqVar('id')." ORDER BY r.id_rating DESC ";
$objDB->query($sSql,'listBR');
while($aR=$objDB->fetch('listBR')) {
?>
	<tr>
		<td class='txt1' colspan='1' align='right'>
			<a href='../crat/rglobVis.php?id=<?=$aR['id_rating']?>&xsn=<?=XSN?>' 
				class='txt1'>&nbsp;<?=$aR['id_rating']?>&nbsp;</a>
		</td>
		<td class='txt1' colspan='5'>
			<a href='../crat/rglobVis.php?id=<?=$aR['id_rating']?>&xsn=<?=XSN?>' 
				class='txt1'>&nbsp;<?=$aR['descr']?></a>
		</td>
		<td class='txt1' colspan='2' align='center'><?
			if ($aR['sigla']=='') {
				echo 'da calc.';
			} else {
				echo $aR['sigla'];
			}
			?>
		</td>
		<td class='txt1' colspan='2' align='center'><?=YMD2DMY($aR['data_rif'])?></td>
		<td class='txt1' colspan='2'><?=$aR['cod_sett']?></td>
		<td class='txt1' colspan='2' align='center'>
			<?
			if ($aR['id_mod_rbil']>0) {
				if ($aR['rb_sigla']!='') {
					echo $aR['rb_sigla'];
				} else {
					echo 'da calc.';
				}
			} else {
				echo 'no';
			}
			?></td>
		<td class='txt1' colspan='2' align='center'>
			<?
			if ($aR['id_mod_rque']>0) {
				if ($aR['rq_sigla']!='') {
					echo $aR['rq_sigla'];
				} else {
					echo 'da comp.';
				}
			} else {
				echo 'no';
			}
			?>
		</td>
		<td class='txt1' colspan='2' align='center'>no</td>
		<td class='txt1' colspan='2' align='center'><?=$aR['stato']?></td>
	</tr>
<?
}
?>
   <tr>
      <td class="cmd1" align="left" colspan='10'><?=$sCmd2L?></td>
      <td class="cmd1" align="right" colspan='10'><?=$sCmd2R?></td>
   </tr>
</table>

<?
codaHtml();
exit();


function confElim() {
	global $objDB;
	$msg = '';
	$sSqlDel = "SELECT * FROM rgl_bilancio  WHERE id_azienda = ".reqVar('id');
	$objDB->exec($sSqlDel);
	if ($acX=$objDB->fetch()) {
		$msg .= "esistono bilanci per questa anagrafica<br>";
	}
	if ($msg != '') {
		return $msg;
	}
	$sSqlDel = "DELETE FROM rgl_azienda WHERE id_azienda = ".reqVar('id');
	$objDB->exec($sSqlDel);
	//return "<pre>".print_r($objDB,1)."</pre>";
	header("Location: ".urlTorna());
	exit();
}
